#ifndef _VFX_LIGHTING_H_
#define _VFX_LIGHTING_H_


#include "graphics/graphics.h"
#include "graphics/image_cache.h"
#include "math/vec2.h"
#include "math/vec4.h"
#include "ui/ui_sprite.h"
#include <vector>

namespace script
{
	class	WriteLua;
};

namespace LuaPlus
{
	class LuaState;	
	class LuaObject;	
}


namespace vfx
{

class	LightingScene;
class	VFXLight
{
public:
	VFXLight(LightingScene* pScene);
	~VFXLight();
	void			Setup(const std::string& File, const math::Vec2& Pos, COLOR c, bool CanResize);
	void			SetActive(bool b) { m_IsActive = b; };
	bool			IsActive() const { return m_IsActive; };
	bool			IsVisible(const math::Vec2& Offset, float	Scale);
	void			Render(const math::Vec2& Offset, float	Scale);
	void			RenderDebug(const math::Vec2& Offset, float	Scale);

	void			SetFlicker(float Speed, float Size) { m_Flicker = true; m_FlickerSpeed = Speed; m_FlickerSize = Size; };
	void			StopFlicker() { m_Flicker = false; };
	void			SetScale(float f) { m_Scale = f; };
	float			GetScale() const { return m_Scale; };
	bool			ShouldFlicker() const { return m_Flicker; };
	float			GetFlickerAmp() const { return m_FlickerSize; };
	float			GetFlickerSpeed() const { return m_FlickerSpeed; };
	const std::string&	GetFile() const { return m_File; };
	bool			ShouldExport() const { return m_ShouldExport; };
	void			SetExport(bool b) { m_ShouldExport = b; };


	LightingScene*	GetScene() { return m_pScene; };
	void			SetPos(const math::Vec2& Pos) { m_Pos = Pos; };
	const math::Vec2&		GetPos() const { return m_Pos; };
	math::Vec4				GetColor();
	void					SetColor(COLOR c) { m_Color = c; };
private:
	LightingScene*	m_pScene;
	math::Vec2		m_Pos, m_Size;
	bool			m_IsActive;
	COLOR			m_Color;
	std::string		m_File;
	ui::UISprite	m_Sprite;

	float			m_Scale;
	bool			m_Flicker;
	float			m_FlickerSpeed;
	float			m_FlickerSize;

	float			m_TimerOffset;
	bool			m_ShouldExport;

	bool			m_CanResize;
};


class	LightingScene
{
public:
	LightingScene();
	~LightingScene();

	void					Init(int	Width = 1024, int Height = 1024, int Depth = 16);
	void					Refresh(const math::Vec2& Offset, float	Scale);
	void					Render();
	void					RenderDebug(const math::Vec2& Offset, float	Scale);

	VFXLight*				CreateLight(const std::string& LightImg, const math::Vec2& Pos, COLOR c = 0xFFFFFFFF, bool CanResize = true);
	void					DropLight(VFXLight* pLight);
	void					ClearAll() { m_Lights.resize(0); };

	void					SaveFile(script::WriteLua& WL, bool CloseComma = false);
	void					LoadFile(LuaPlus::LuaState& Obj);
	void					EnableStencilTest(bool b);
	void					RenderToStencil(const math::Vec2& Offset);
	int						GetWidth() const { return m_Width; };
	int						GetHeight() const { return m_Height; };
	int						GetDepth() const { return m_Depth; };
	void					SetClearColor(COLOR ClearColor) { m_ClearColor = ClearColor; }

private:
	ImageCache*				m_pCache;
	int						m_Width, m_Height, m_Depth;

	std::vector<VFXLight*>	m_Lights;

	COLOR				m_ClearColor;
};

};

#endif // _VFX_LIGHTING_H_